package rabitmq

import (
	"gitee.com/lv_baobao/gcore"
	"sync"
)

//bus
type MqBus struct {
	producer *rabbitMqProducer
	consumer *RabbitMqConsumer
}

type ExchangeOption struct {
	Name       string
	Type       string
	Durable    bool
	AutoDelete bool
	Arguments  map[string]interface{}
}

type QueueOption struct {
	Name       string
	RoutingKey string
	Durable    bool
	AutoDelete bool
	Exclusive  bool
	Arguments  map[string]interface{}
}

var rabbitMqConfig = gcore.RabbitMqConfig{}

func init() {
	rabbitMqConfig = gcore.NewAppSettingsHelper().GetAppConfig().RabbitMq
}

var bus *MqBus
var once sync.Once

func NewMqEventBus() *MqBus {
	//once.Do(func() {
	bus = &MqBus{producer: NewRabbitMqProducer(), consumer: new(RabbitMqConsumer)}
	//})
	return bus
}

func (bus *MqBus) PublishDirect(obj interface{}, exchangeOpt ExchangeOption, queueOpt QueueOption, expire int) error {
	return bus.producer.PublishDirect(obj, exchangeOpt, queueOpt, expire)
}

func (bus *MqBus) PublishFanout(obj interface{}, exchangeOpt ExchangeOption) error {
	return bus.producer.PublishFanout(obj, exchangeOpt)
}

func (bus MqBus) SubscribeDirect(exchangeOpt ExchangeOption, queueOpt QueueOption, f func([]byte) bool) {
	bus.consumer.SubscribeDirect(exchangeOpt, queueOpt, f)
}

func (bus *MqBus) SubscribeFanout(queueName string, exchangeOpt ExchangeOption, f func([]byte)) {
	bus.consumer.SubscribeFanout(queueName, exchangeOpt, f)
}
